<template>
  <div class="managetteamview-box uploadQuestion_parent">
    <a-card :title="team.title">
      <a-row>
        <a-col :span="22">
          <h2 style="margin-left: 35px">
            {{ team.title }}
          </h2>
        </a-col>
        <a-col :span="2">
          <a-button @click="delTeam" type="danger">
            删除
          </a-button>
        </a-col>
      </a-row>

      <a-row >
        <a-col :span="3" class="font_cent" >标题</a-col>
        <a-col :span="12">
          <a-input v-model="team.title" />
        </a-col>
      </a-row>
      <a-space style="margin-top: 20px"></a-space>
      <a-row>
        <a-col :span="3" class="font_cent">标签</a-col>
        <a-col :span="12">
          <InputTag
            allowClear
            size='large'
            v-model="team.tags"
            :style="{ maxWidth: 350, marginRight: 20 }"
            class="tagClass"
          />
        </a-col>
      </a-row>
      <a-space style="margin-top: 20px"></a-space>
      <a-row>
        <a-col :span="3" class="font_cent">判题配置</a-col>
        <a-col :span="21">
          <a-row>
            <a-col :span="3"  class="font_cent">
              时间限制
            </a-col>
            <a-col :span="10">
              <a-input v-model="judgeconfig.timeLimit" size="large" :style="{ maxWidth: 250, marginRight: 20 }"/>
            </a-col>
          </a-row>
          <a-space style="margin-top: 20px"></a-space>
          <a-row>
            <a-col :span="3"  class="font_cent">
              内存限制
            </a-col>
            <a-col :span="10">
              <a-input v-model="judgeconfig.memoryLimit" size="large" :style="{ maxWidth: 250, marginRight: 20 }"/>
            </a-col>
          </a-row>
          <a-space style="margin-top: 20px"></a-space>
          <a-row>
            <a-col :span="3"  class="font_cent">
              堆栈限制
            </a-col>
            <a-col :span="10">
              <a-input v-model="judgeconfig.stackLimit" size="large" :style="{ maxWidth: 250, marginRight: 20 }"/>
            </a-col>
          </a-row>
        </a-col>
      </a-row>
      <a-space style="margin-top: 20px"></a-space>
      <a-row>
        <a-col :span="4"></a-col>
        <a-col :span="9">
          <a-row :gutter="15">
            <a-col :span="8"  class="font_cent">
              输入用例
            </a-col>
            <a-col :span="16">
              <a-upload :custom-request="readUploadInput" :multiple="false"/>
            </a-col>
          </a-row>
        </a-col>
        <a-col :span="9">
          <a-row :gutter="15">
            <a-col :span="8"  class="font_cent">
              输出用例
            </a-col>
            <a-col :span="16">
              <a-upload :custom-request="readUploadOutput" :multiple="false" />
            </a-col>
          </a-row>
        </a-col>
      </a-row>
      <a-space style="margin-top: 20px"></a-space>
      <a-row>
        <a-col :span="3" class="font_cent">题目内容</a-col>
        <a-col :span="21">
          <MdEditor :data="team.content" @setData="setTeamContent"/>
        </a-col>
      </a-row>
      <div >
        <a-button @click="submitUpdate"  class="uploadQuestion_box_btn" type="primary"> 提交 </a-button>
      </div>
    </a-card>

  </div>
</template>

<script setup lang="ts">
import {  onMounted, ref, defineEmits } from "vue";
import { QuestionControllerService, QuestionSubmitControllerService } from "../../generated";
import { Message, InputTag } from "@arco-design/web-vue";
import { useRoute, useRouter } from "vue-router";
import MdEditor from "@/components/MdEditor.vue";
import store from "@/store";

const route = useRoute();
const router = useRouter();
const team = ref({});
const tpInput = ref('');
const tpOutput = ref('');
const judgeconfig = ref({
  memoryLimit: '',
  timeLimit: '',
  stackLimit: '',
})
let qid:number;
onMounted(async () => {
  // 请求题目
  qid = route.query.id;
  let res = await QuestionControllerService.getQuestionByIdUsingGet(qid);
  console.log('tesm=res', res.data);
  if(res.code === 0) {
    team.value = res.data;
    judgeconfig.value = team.value?.judgeconfig;
  }else {
    Message.error("系统错误");
    // 返回主页面
    router.push({path: '/'})
  }
})
const readUploadInput = async (options) => {
  let res = await QuestionSubmitControllerService.readFileUsingPost(options?.fileItem?.file);
  if (res.code == 0) {
      // 拿到文件内容
      tpInput.value = res.data;
  }else {
    Message.error("文件不合法");
  }
};
const readUploadOutput = async (options) => {
  let res = await QuestionSubmitControllerService.readFileUsingPost(options?.fileItem?.file);
  if (res.code == 0) {
     // 拿到文件内容
     tpOutput.value = res.data;
  }else {
    Message.error("文件不合法");
  }
};

const submitUpdate = async () => {
  let judgecase = JSON.stringify({input: tpInput.value, output: tpOutput.value});
  let judgeconfig = JSON.stringify({
      memoryLimit: (team.value?.judgeconfig?.memoryLimit ?? '10') + '',
      stackLimit: (team.value?.judgeconfig?.stackLimit ?? '10') + '',
    timeLimit: (team.value?.judgeconfig?.timeLimit ?? '10') + ''
  });
  console.log('judgeconfig', judgeconfig);
  console.log('judgecase', judgecase);
  if(!team.value?.answer) {
    Message.error("答案不能为空");
    return;
  }
  if(!team.value?.content) {
    Message.error("内容不能为空");
    return;
  }
  if(!team.value?.id) {
    Message.error("id不能为空");
    return;
  }
  if(!team.value?.title) {
    Message.error("标题不能为空");
    return;
  }
  if(!store.state.user.curUser?.id) {
    Message.error("用户未登录");
    return;
  }
  let tags = JSON.stringify(team.value?.tags ?? {});
  let res = await QuestionControllerService.updateQuestionByIdUsingPost(
    0, //      acceptednum?: number,
    team.value?.answer,
    team.value?.content,
    0,  // favournum
    team.value?.id, // id
    judgecase,// judgecase
    judgeconfig, // judgeconfig
    0, // submitnum
    tags, // tags
    0, // thumbnum
    team.value.title,
    store.state.user.curUser?.id,
  );
  if (res.code === 0) {
    Message.success('添加成功')
    // 跳转到主页
    router.push({path: '/'})
  }else {
    Message.error('添加失败')
  }
}
const setTeamContent = (v:string) => {
  team.value = {...team.value, content: v};
  console.log('team.value', team.value);
}

const delTeam = async () => {
  let res = await QuestionControllerService.deleteQuestionByIdUsingDelete(qid);
  if (res.code == 0) {
    Message.error("删除成功");
    // 跳转回主页
    router.push({
      path: '/'
    })
  }else {
    Message.error("删除失败");
  }
}
</script>

<style scoped>
.managetteamview-box{
  height: 100%;
  width: 100%;
}
.font_cent {
  text-align: center;
  font-weight: bold;
}
.tagClass {
  color: green;
}
.uploadQuestion_parent {
  position: relative;
}
.uploadQuestion_box_btn {
  position: absolute;
  bottom: 7%;
  left: 85%;
}
</style>